package com.study.d1_stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

public class Test3 {
    public static void main(String[] args) {
        //目标：了解Stream常用中间方法：中间方法指的是调用完成后会返回新的Stream流，可以继续使用（支持链式编程）
        List<String> list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("张三丰");
        //1、过滤方法
        list.stream().filter(s -> s.startsWith("张")).forEach(System.out::println);
        //准备一个集合排序，对象类实现Comparable接口，指定比较规则，sorted就可以按规则排序，否则报错
        List<Movie> movies = new ArrayList<>();
        movies.add(new Movie(1,"哈哈",20.5,"成龙"));
        movies.add(new Movie(2,"2哈",200.5,"a1龙"));
        movies.add(new Movie(4,"哈3哈",26.5,"s龙"));
        movies.add(new Movie(5,"1哈哈",20.5,"d龙"));
        movies.add(new Movie(3,"哈4哈",0.5,"fds龙"));
        movies.add(new Movie(3,"哈4哈",0.5,"fds龙"));
//        movies.stream().sorted().forEach(System.out::println);
        movies.stream().sorted(Comparator.comparingDouble(Movie::getPrice)).forEach(System.out::println);
        System.out.println("====================================");
        //limit取前几个
        movies.stream().sorted().limit(3).forEach(System.out::println);
        System.out.println("======================================");
        //去重：需要重写类的equals和hashcode方法
        movies.stream().sorted().distinct().forEach(System.out::println);
        System.out.println("======================================");
        //map加工方法：把流上的数据加工成新数据
        movies.stream().sorted().distinct().map(m->m.getName()+"==>"+m.getPrice()).forEach(System.out::println);
        System.out.println("======================================");
        //合并流：把两个流连接起来
        Stream<String> s1 = Stream.of("哈哈", "呵呵");
        Stream<String> s2 = Stream.of("嘿嘿");
        Stream<String> concat = Stream.concat(s1, s2);
        System.out.println(concat.count());
    }
}
